package com.ry.novel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ry.novel.enums.NovelConst;
import com.ry.novel.mapper.ReportMapper;
import com.ry.novel.model.dto.BookIndexInfo;
import com.ry.novel.model.dto.BookInfo;
import com.ry.novel.model.dto.ReportInfo;
import com.ry.novel.model.dto.UserInfo;
import com.ry.novel.model.dto.condition.ReportQuery;
import com.ry.novel.model.entity.BookIndex;
import com.ry.novel.model.entity.Report;
import com.ry.novel.service.BookIndexService;
import com.ry.novel.service.BookService;
import com.ry.novel.service.ReaderService;
import com.ry.novel.service.ReportService;
import com.ry.novel.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ry
 * @since 2022-04-08 22:58
 **/
@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private ReaderService readerService;

    @Autowired
    private BookService bookService;

    @Autowired
    private BookIndexService bookIndexService;

    @Override
    public Page<ReportInfo> queryReport(ReportQuery reportQuery) {
        Page<Report> page = new Page<>(reportQuery.getPage(),reportQuery.getSize());
        Page<ReportInfo> infoPage = new Page<>(reportQuery.getPage(),reportQuery.getSize());
        QueryWrapper<Report> wrapper = new QueryWrapper<>();

        reportMapper.selectPage(page, wrapper);
        List<ReportInfo> infos = page.getRecords().stream().map(entity -> {
            ReportInfo reportInfo = new ReportInfo();
            reportInfo.setId(entity.getId());
            reportInfo.setUserId(entity.getUserId());
            UserInfo user = readerService.queryById(entity.getUserId());
            if (null != user){
                reportInfo.setUsername(user.getUsername());
            }
            reportInfo.setBookId(entity.getBookId());
            BookInfo book = bookService.searchById(entity.getBookId());
            if (null != book){
                reportInfo.setBookName(book.getBookName());
            }
            reportInfo.setBookIndexId(entity.getBookIndexId());
            BookIndexInfo bookIndex = bookIndexService.queryBookIndexById(entity.getBookIndexId());
            if (null != bookIndex){
                reportInfo.setBookIndexName(bookIndex.getIndexName());
            }
            reportInfo.setContent(entity.getContent());
            reportInfo.setCreateTime(DateUtil.format(entity.getCreateTime()));
            reportInfo.setHandlerTime(DateUtil.format(entity.getHandlerTime()));
            reportInfo.setState(entity.getState());

            return reportInfo;
        }).collect(Collectors.toList());

        infoPage.setRecords(infos);
        infoPage.setTotal(page.getTotal());

        return infoPage;
    }

    @Override
    public void addReport(ReportInfo info) {
        Report report = new Report();
        report.setUserId(info.getUserId());
        report.setBookId(info.getBookId());
        report.setBookIndexId(info.getBookIndexId());
        report.setContent(info.getContent());
        report.setCreateTime(LocalDateTime.now().toString());
        report.setState(NovelConst.REPORT_NOT_HANDLE);

        reportMapper.insert(report);
    }

    @Override
    public void updateReport(ReportInfo info) {
        Report report = new Report();
        report.setId(info.getId());
        report.setUserId(info.getUserId());
        report.setBookId(info.getBookId());
        report.setBookIndexId(info.getBookIndexId());
        report.setContent(info.getContent());
        if (info.getState() != null){
            report.setState(info.getState());
            report.setHandlerTime(LocalDateTime.now().toString());
        }

        reportMapper.updateById(report);
    }

    @Override
    public void deleteReport(Long id) {
        reportMapper.deleteById(id);
    }
}
